﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ManageSystem
{
    public partial class LoginForm : Form


    {
        static int count = 0;
        public LoginForm()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void userPswd_TextChanged(object sender, EventArgs e)
        {

        }

        private void loginBtn_Click(object sender, EventArgs e)

        {
            if (userName.Text == "")
            {
                MessageBox.Show("请输入用户名!");
                return;
            }
            else if (userPswd.Text == "")
            {
                MessageBox.Show("请输入密码!");
                return;
            }
            if (count < 3)
            {
                count++;

                string commend = "select * from UserInfo where UserName=N'" + this.userName.Text + "' and UserPwd='" + this.userPswd.Text + "'";


                SqlDataReader sqlDr = Utils.getDataReader(commend);

                if (sqlDr.Read())
                {
                    int auth = (int)sqlDr["UserAuth"];
                    if (auth == 1)
                    {
                        MessageBox.Show("登陆成功！");
                        ManageForm manageForm = new ManageForm(sqlDr["UserId"].ToString(), sqlDr["UserName"].ToString());
                        manageForm.Show();
                        manageForm.Owner = this;
                        count = 0;
                        this.warnMsg.Text = "";
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("登陆成功！");
                        VisitorForm visitorForm = new VisitorForm(sqlDr["UserId"].ToString(), sqlDr["UserName"].ToString());
                        visitorForm.Owner = this;
                        visitorForm.Show();
                        count = 0;
                        this.warnMsg.Text = "";
                        this.Hide();
                    }
                }
                else
                {
                    MessageBox.Show("登陆失败！");
                    this.warnMsg.Text = "用户名或密码错误，请重试！";
                    //this.userName.Text = "";
                    //this.userPswd.Text = "";
                    userPswd.Focus();
                }
                sqlDr.Close();
            }
            else
            {
                MessageBox.Show("你已连续三次输入密码错误，将自动退出系统！");
                Application.Exit();


            }
        }

        private void finishBtn_Click(object sender, EventArgs e)
        {
            Application.Exit();

        }

        private void LoginForm_Load(object sender, EventArgs e)
        {


        }

        private void regeist_Click(object sender, EventArgs e)
        {
            RegeistUser regeist = new RegeistUser();
            regeist.Show();
        }
    }


}
